/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package kbcquiz;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.File;
import javax.swing.JFrame;
import library.Configuration;

import frame.Admin;
import frame.Login;
import frame.Main;
import frame.Participate;
import frame.Play;
import frame.Register;
import frame.Configurate;
import frame.AdminNewQA;
import frame.AdminUser;
import frame.Report;


public class App {
    static Configuration config;
    static Main mainFrame;
    static Login loginFrame;
    static Admin adminFrame;
    static AdminUser adminuserframe;
    static Participate participateFrame;
    static Play playFrame;
    static JFrame currentDisplay = null;
    static AdminNewQA AdminNewQA;
    static Register RegisterFrame;
    static Report reportFrame;
    static Configurate configurateFrame;
    static final String configFile = "/Config.properties";
    static public String runTimePath = "";
    static Boolean isLogin = true;
    static public String userName = "";
    static public int userID;
    static String jarFile = "KBCQuiz.jar";
    static public int playerCurrentPoint;
    static public int adminUserID = 0;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                runTimePath = App.class.getResource("App.class").getPath().replaceAll("App.class", "");
                if (runTimePath.indexOf(jarFile + "!") >= 0) {
                    String[] t = runTimePath.split(jarFile + "!");
                    runTimePath = t[0].substring(6) + File.separator;
                    if (System.getProperty("os.name").indexOf("Linux") >= 0) {
                        runTimePath = File.separator + runTimePath;
                    }
                }
                runTimePath = runTimePath.replace("%20", " ");
                //setDisplay("admin");
                setDisplay("MainFrame");
            }
        });

    }

    public static AdminUser getAdminUserFrame(){
        if(adminuserframe == null){
            adminuserframe = new AdminUser();
        }
        return adminuserframe;
    }
    public static Admin getAdminFrame() {
        if (adminFrame == null) {
            adminFrame = new Admin();
        }
        return adminFrame;
    }

    public static Configuration getConfig() {
        if (config == null) {
            config = new Configuration(runTimePath + configFile);
        }
        return config;
    }

    public static Participate getParticipateFrame() {
        if (participateFrame == null) {
            participateFrame = new Participate();
        }
        participateFrame.resetField();
        return participateFrame;
    }

    public static Login getLoginFrame() {
        if (loginFrame == null) {
            loginFrame = new Login();
        }
        //loginFrame.resetField();
        return loginFrame;
    }

    public static AdminNewQA getAdminNewQA() {
        if (AdminNewQA == null) {
            AdminNewQA = new AdminNewQA();
        }
        return AdminNewQA;
    }

    public static Register getRegisterFrame() {
        if (RegisterFrame == null) {
            RegisterFrame = new Register();
        }
        return RegisterFrame;
    }

    public static Main getMainFrame() {
        if (mainFrame == null) {
            mainFrame = new Main();
        }
        return mainFrame;
    }

    public static Play getPlayFrame() {
        if (playFrame == null) {
            playFrame = new Play();
        }
        return playFrame;
    }

    public static JFrame getReportFrame() {
        if (reportFrame == null) {
            reportFrame = new Report();
        }
        return reportFrame;
    }
    
     public static Configurate getConfigurateFrame() {
        if (configurateFrame == null) {
            configurateFrame = new Configurate();
        }
        return configurateFrame;
    }

    public static JFrame setDisplay(JFrame frame) {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int w = frame.getSize().width;
        int h = frame.getSize().height;
        int x = (screenSize.width - w) / 2;
        int y = (screenSize.height - h) / 2;
        // Move the window
        frame.setLocation(x, y);
        frame.setVisible(true);
        currentDisplay = frame;
        return frame;
    }

    public static JFrame setDisplay(String framename) {
        framename = framename.toLowerCase();
        if (framename.equals("main") || framename.equals("mainframe")) {
            return setDisplay(getMainFrame());
        } else if (framename.equals("login") || framename.equals("loginframe")) {
            return setDisplay(getLoginFrame());
        } else if (framename.equals("admin") || framename.equals("adminframe")) {
            return setDisplay(getAdminFrame());
        } else if (framename.equals("participate") || framename.equals("part") || framename.equals("participateframe")) {
            return setDisplay(getParticipateFrame());
        } else if (framename.equals("configurate") || framename.equals("configurateframe")) {
            return setDisplay(getConfigurateFrame());    
        } else if (framename.equals("newqa") || framename.equals("newqaframe")) {
            return setDisplay(getAdminNewQA());
        } else if (framename.equals("register") || framename.equals("registerframe")) {
            return setDisplay(getRegisterFrame());
        } else if (framename.equals("report") || framename.equals("reportframe")) {
            return setDisplay(getReportFrame());
        } else if (framename.equals("play") || framename.equals("playframe")) {
            return setDisplay(getPlayFrame());
        } else if(framename.equals("adminuser")){
            return setDisplay(getAdminUserFrame());
        }
        else {

            return null;
        }
    }

    public static JFrame switchDisplay(JFrame frm) {
        if (currentDisplay != null) {
            currentDisplay.setVisible(false);
        }
        setDisplay(frm);
        return frm;
    }

    public static JFrame switchDisplay(String frm) {
        if (currentDisplay != null) {
            currentDisplay.setVisible(false);
        }
        setDisplay(frm);
        return currentDisplay;
    }

    public static JFrame getCurrentDisplay() {
        return currentDisplay;
    }

    public static void setLogin(String username) {
        userName = username;
        isLogin = true;
    }

    public static void setLogout() {
        userName = "";
        isLogin = false;
    }

    public static Boolean IsLogin() {
        return isLogin;
    }
}
